﻿using System;
using System.IdentityModel.Claims;
using System.Web.UI.WebControls;
using Altairis.IdentityToolkit;
using Altairis.IdentityToolkit.LiveId;
using Altairis.IdentityToolkit.PersonalCard;

public partial class Auth_Default : System.Web.UI.Page {
    private LiveIdIdentityLinkProvider liveProvider;
    private PersonalCardIdentityLinkProvider cardProvider;

    protected void Page_Load(object sender, EventArgs e) {
        // Find providers for LiveID and InfoCard
        this.liveProvider = IdentityLinkManager.Providers["LiveIdProvider"] as LiveIdIdentityLinkProvider;
        this.cardProvider = IdentityLinkManager.Providers["InfoCardProvider"] as PersonalCardIdentityLinkProvider;

        // Bind GridViews
        this.GridViewLive.DataSource = this.liveProvider.GetLinksForUser();
        this.GridViewLive.DataBind();
        this.GridViewCard.DataSource = this.cardProvider.GetLinksForUser();
        this.GridViewCard.DataBind();
    }

    protected void InfoCardSelector1_TokenSubmitted(object sender, LeastPrivilege.CardSpaceLibrary.UI.TokenSubmittedEventArgs e) {
        // Get card's first ClaimSet
        ClaimSet cs = e.Token.AuthorizationContext.ClaimSets[0];

        // Find provider for InfoCards
        var provider = IdentityLinkManager.Providers.Find(cs);

        // Create link between given card and membership user
        provider.CreateLink(cs, this.User.Identity.Name);

        Response.Redirect("Default.aspx");
    }

    protected void GridViewLive_RowCommand(object sender, GridViewCommandEventArgs e) {
        if (e.CommandName.Equals("RemoveLive")) {
            this.liveProvider.RemoveLink(new Guid(e.CommandArgument as string), this.User.Identity.Name);
            Response.Redirect("Default.aspx");
        }
    }

    protected void GridViewCard_RowCommand(object sender, GridViewCommandEventArgs e) {

        if (e.CommandName.Equals("RemoveCard")) {
            this.cardProvider.RemoveLink(e.CommandArgument as string, this.User.Identity.Name);
            Response.Redirect("Default.aspx");
        }
    }

}
